﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Text.RegularExpressions;
using System.Text;
using System.Collections; 

namespace PM.Code.Basic
{
    /// <summary>
    /// 基础操作类
    /// </summary>
    public static class pmbasic
    {
        /// <summary>
        /// 取参数 
        /// </summary>
        /// <param name="QueryName">参数名称</param>
        /// <returns></returns>
        public static string GetQueryString(string QueryName)
        {
            if (HttpContext.Current.Request.QueryString[QueryName] != null)
            {
                return HttpContext.Current.Request.QueryString[QueryName];
            }
            return string.Empty;
        }
        /// <summary>
        /// 取Session值 
        /// </summary>
        /// <param name="SessionName">Session名称</param>
        /// <returns></returns>
        public static string GetSessionByName(string SessionName)
        {
            if (HttpContext.Current.Session[SessionName] != null)
            {
                return (string)HttpContext.Current.Session[SessionName];
            }
            return string.Empty;
        }
        /// <summary>
        /// 从web.config取配置信息
        /// </summary>
        /// <param name="key">key </param>
        /// <returns></returns>
        public static string GetWebConfigString(string key)
        {
            return ConfigurationSettings.AppSettings[key].Trim();
        }
        /// <summary>
        /// 在母串中查找子串出现的次数。
        /// </summary>
        /// <param name="MainStr"></param>
        /// <param name="ChildStr"></param>
        /// <returns></returns>
        public static int MatchingCount(string MainStr, string ChildStr)
        {
            MainStr = MainStr.ToLower();
            ChildStr = ChildStr.ToLower();
            Regex regex = new Regex(ChildStr);
            return regex.Matches(MainStr).Count;
        }
        /// <summary>
        /// 截取字符串。不足以 "..."返回
        /// </summary>
        /// <param name="str">要接取得字符串</param>
        /// <param name="len">保留的字节数</param>
        /// <returns></returns>
        public static string strCal(string str, int len)
        {
            byte[] bytes = Encoding.Default.GetBytes(str);
            if (bytes.Length > len)
            {
                return (Encoding.Default.GetString(bytes, 0, len - 2) + "...");
            }
            return str;
        }
        /// <summary>
        /// 倒转字符串 
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static string strreverse(string str)
        {
            int length = str.Length;
            string str2 = "";
            if (4 != 0)
            {
            }
            for (int i = length - 1; i >= 0; i--)
            {
                str2 = str2 + str.Substring(i, 1);
            }
            return str2;
        }
        /// <summary>
        /// 替换html中的特殊字符
        /// </summary>
        /// <param name="theString">需要进行替换的文本。</param>
        /// <returns>替换完的文本。</returns>
        public static string HtmlEncode(string theString)
        {
            theString = theString.Replace(">", "&gt;");
            theString = theString.Replace("<", "&lt;");
            theString = theString.Replace(" ", "&nbsp;");
            theString = theString.Replace(" ", "&nbsp;");
            theString = theString.Replace("\"", "&quot;");
            theString = theString.Replace("\'", "'");
            theString = theString.Replace("\n", "<br/> ");
            return theString;
        }
        /// <summary>
        /// 恢复html中的特殊字符
        /// </summary>
        /// <param name="theString">需要恢复的文本。</param>
        /// <returns>恢复好的文本。</returns>
        public static string HtmlDiscode(string theString)
        {
            theString = theString.Replace("&gt;", ">");
            theString = theString.Replace("&lt;", "<");
            theString = theString.Replace("&nbsp;", " ");
            theString = theString.Replace("&nbsp;", " ");
            theString = theString.Replace("&quot;", "\"");
            theString = theString.Replace("'", "\'");
            theString = theString.Replace("<br/> ", "\n");
            return theString;
        }
    }
}
